﻿#region Using directives

using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using Pharmacy.Entities;
using Pharmacy.Data;

#endregion

namespace Pharmacy.Data.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="CmsPatientProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract class CmsPatientProviderBaseCore : EntityViewProviderBase<CmsPatient>
	{
		#region Custom Methods
		
		
		#region _CMSPatient_Get
		
		/// <summary>
		///	This method wrap the '_CMSPatient_Get' stored procedure. 
		/// </summary>
		/// <param name="patientCode"> A <c>System.String</c> instance.</param>
		/// <remark>This method is generate from a stored procedure.</remark>
		/// <returns>A <see cref="VList&lt;CmsPatient&gt;"/> instance.</returns>
		public VList<CmsPatient> Get(System.String patientCode)
		{
			return Get(null, 0, int.MaxValue , patientCode);
		}
		
		/// <summary>
		///	This method wrap the '_CMSPatient_Get' stored procedure. 
		/// </summary>
		/// <param name="patientCode"> A <c>System.String</c> instance.</param>
		/// <param name="start">Row number at which to start reading.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remark>This method is generate from a stored procedure.</remark>
		/// <returns>A <see cref="VList&lt;CmsPatient&gt;"/> instance.</returns>
		public VList<CmsPatient> Get(int start, int pageLength, System.String patientCode)
		{
			return Get(null, start, pageLength , patientCode);
		}
				
		/// <summary>
		///	This method wrap the '_CMSPatient_Get' stored procedure. 
		/// </summary>
		/// <param name="patientCode"> A <c>System.String</c> instance.</param>
		/// <remark>This method is generate from a stored procedure.</remark>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <returns>A <see cref="VList&lt;CmsPatient&gt;"/> instance.</returns>
		public VList<CmsPatient> Get(TransactionManager transactionManager, System.String patientCode)
		{
			return Get(transactionManager, 0, int.MaxValue , patientCode);
		}
		
		/// <summary>
		///	This method wrap the '_CMSPatient_Get' stored procedure. 
		/// </summary>
		/// <param name="patientCode"> A <c>System.String</c> instance.</param>
		/// <param name="start">Row number at which to start reading.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <remark>This method is generate from a stored procedure.</remark>
		/// <returns>A <see cref="VList&lt;CmsPatient&gt;"/> instance.</returns>
		public abstract VList<CmsPatient> Get(TransactionManager transactionManager, int start, int pageLength, System.String patientCode);
		
		#endregion

		
		#endregion

		#region Helper Functions
		
		/*
		///<summary>
		/// Fill an VList&lt;CmsPatient&gt; From a DataSet
		///</summary>
		/// <param name="dataSet">the DataSet</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Start row</param>
		/// <param name="pagelen">number of row.</param>
		///<returns><see chref="VList&lt;CmsPatient&gt;"/></returns>
		protected static VList&lt;CmsPatient&gt; Fill(DataSet dataSet, VList<CmsPatient> rows, int start, int pagelen)
		{
			if (dataSet.Tables.Count == 1)
			{
				return Fill(dataSet.Tables[0], rows, start, pagelen);
			}
			else
			{
				return new VList<CmsPatient>();
			}	
		}
		
		
		///<summary>
		/// Fill an VList&lt;CmsPatient&gt; From a DataTable
		///</summary>
		/// <param name="dataTable">the DataTable that hold the data.</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Start row</param>
		/// <param name="pagelen">number of row.</param>
		///<returns><see chref="VList<CmsPatient>"/></returns>
		protected static VList&lt;CmsPatient&gt; Fill(DataTable dataTable, VList<CmsPatient> rows, int start, int pagelen)
		{
			int recordnum = 0;
			
			System.Collections.IEnumerator dataRows =  dataTable.Rows.GetEnumerator();
			
			while (dataRows.MoveNext() && (pagelen != 0))
			{
				if(recordnum >= start)
				{
					DataRow row = (DataRow)dataRows.Current;
				
					CmsPatient c = new CmsPatient();
					c.PatientCode = (Convert.IsDBNull(row["PatientCode"]))?string.Empty:(System.String)row["PatientCode"];
					c.MemberType = (Convert.IsDBNull(row["MemberType"]))?string.Empty:(System.String)row["MemberType"];
					c.JobAddress = (Convert.IsDBNull(row["JobAddress"]))?string.Empty:(System.String)row["JobAddress"];
					c.Company = (Convert.IsDBNull(row["Company"]))?string.Empty:(System.String)row["Company"];
					c.DateOfBirth = (Convert.IsDBNull(row["DateOfBirth"]))?DateTime.MinValue:(System.DateTime?)row["DateOfBirth"];
					c.FullName = (Convert.IsDBNull(row["FullName"]))?string.Empty:(System.String)row["FullName"];
					c.Gender = (Convert.IsDBNull(row["Gender"]))?string.Empty:(System.String)row["Gender"];
					c.Nationality = (Convert.IsDBNull(row["Nationality"]))?string.Empty:(System.String)row["Nationality"];
					c.AcceptChanges();
					rows.Add(c);
					pagelen -= 1;
				}
				recordnum += 1;
			}
			return rows;
		}
		*/	
						
		///<summary>
		/// Fill an <see cref="VList&lt;CmsPatient&gt;"/> From a DataReader.
		///</summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Start row</param>
		/// <param name="pageLength">number of row.</param>
		///<returns>a <see cref="VList&lt;CmsPatient&gt;"/></returns>
		protected VList<CmsPatient> Fill(IDataReader reader, VList<CmsPatient> rows, int start, int pageLength)
		{
			int recordnum = 0;
			while (reader.Read() && (pageLength != 0))
			{
				if(recordnum >= start)
				{
					CmsPatient entity = null;
					if (DataRepository.Provider.UseEntityFactory)
					{
						entity = EntityManager.CreateViewEntity<CmsPatient>("CmsPatient",  DataRepository.Provider.EntityCreationalFactoryType); 
					}
					else
					{
						entity = new CmsPatient();
					}
					
					entity.SuppressEntityEvents = true;

					entity.PatientCode = (reader.IsDBNull(((int)CmsPatientColumn.PatientCode)))?null:(System.String)reader[((int)CmsPatientColumn.PatientCode)];
					//entity.PatientCode = (Convert.IsDBNull(reader["PatientCode"]))?string.Empty:(System.String)reader["PatientCode"];
					entity.MemberType = (reader.IsDBNull(((int)CmsPatientColumn.MemberType)))?null:(System.String)reader[((int)CmsPatientColumn.MemberType)];
					//entity.MemberType = (Convert.IsDBNull(reader["MemberType"]))?string.Empty:(System.String)reader["MemberType"];
					entity.JobAddress = (reader.IsDBNull(((int)CmsPatientColumn.JobAddress)))?null:(System.String)reader[((int)CmsPatientColumn.JobAddress)];
					//entity.JobAddress = (Convert.IsDBNull(reader["JobAddress"]))?string.Empty:(System.String)reader["JobAddress"];
					entity.Company = (reader.IsDBNull(((int)CmsPatientColumn.Company)))?null:(System.String)reader[((int)CmsPatientColumn.Company)];
					//entity.Company = (Convert.IsDBNull(reader["Company"]))?string.Empty:(System.String)reader["Company"];
					entity.DateOfBirth = (reader.IsDBNull(((int)CmsPatientColumn.DateOfBirth)))?null:(System.DateTime?)reader[((int)CmsPatientColumn.DateOfBirth)];
					//entity.DateOfBirth = (Convert.IsDBNull(reader["DateOfBirth"]))?DateTime.MinValue:(System.DateTime?)reader["DateOfBirth"];
					entity.FullName = (reader.IsDBNull(((int)CmsPatientColumn.FullName)))?null:(System.String)reader[((int)CmsPatientColumn.FullName)];
					//entity.FullName = (Convert.IsDBNull(reader["FullName"]))?string.Empty:(System.String)reader["FullName"];
					entity.Gender = (reader.IsDBNull(((int)CmsPatientColumn.Gender)))?null:(System.String)reader[((int)CmsPatientColumn.Gender)];
					//entity.Gender = (Convert.IsDBNull(reader["Gender"]))?string.Empty:(System.String)reader["Gender"];
					entity.Nationality = (reader.IsDBNull(((int)CmsPatientColumn.Nationality)))?null:(System.String)reader[((int)CmsPatientColumn.Nationality)];
					//entity.Nationality = (Convert.IsDBNull(reader["Nationality"]))?string.Empty:(System.String)reader["Nationality"];
					entity.AcceptChanges();
					entity.SuppressEntityEvents = false;
					
					rows.Add(entity);
					pageLength -= 1;
				}
				recordnum += 1;
			}
			return rows;
		}
		
		
		/// <summary>
		/// Refreshes the <see cref="CmsPatient"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="CmsPatient"/> object to refresh.</param>
		protected void RefreshEntity(IDataReader reader, CmsPatient entity)
		{
			reader.Read();
			entity.PatientCode = (reader.IsDBNull(((int)CmsPatientColumn.PatientCode)))?null:(System.String)reader[((int)CmsPatientColumn.PatientCode)];
			//entity.PatientCode = (Convert.IsDBNull(reader["PatientCode"]))?string.Empty:(System.String)reader["PatientCode"];
			entity.MemberType = (reader.IsDBNull(((int)CmsPatientColumn.MemberType)))?null:(System.String)reader[((int)CmsPatientColumn.MemberType)];
			//entity.MemberType = (Convert.IsDBNull(reader["MemberType"]))?string.Empty:(System.String)reader["MemberType"];
			entity.JobAddress = (reader.IsDBNull(((int)CmsPatientColumn.JobAddress)))?null:(System.String)reader[((int)CmsPatientColumn.JobAddress)];
			//entity.JobAddress = (Convert.IsDBNull(reader["JobAddress"]))?string.Empty:(System.String)reader["JobAddress"];
			entity.Company = (reader.IsDBNull(((int)CmsPatientColumn.Company)))?null:(System.String)reader[((int)CmsPatientColumn.Company)];
			//entity.Company = (Convert.IsDBNull(reader["Company"]))?string.Empty:(System.String)reader["Company"];
			entity.DateOfBirth = (reader.IsDBNull(((int)CmsPatientColumn.DateOfBirth)))?null:(System.DateTime?)reader[((int)CmsPatientColumn.DateOfBirth)];
			//entity.DateOfBirth = (Convert.IsDBNull(reader["DateOfBirth"]))?DateTime.MinValue:(System.DateTime?)reader["DateOfBirth"];
			entity.FullName = (reader.IsDBNull(((int)CmsPatientColumn.FullName)))?null:(System.String)reader[((int)CmsPatientColumn.FullName)];
			//entity.FullName = (Convert.IsDBNull(reader["FullName"]))?string.Empty:(System.String)reader["FullName"];
			entity.Gender = (reader.IsDBNull(((int)CmsPatientColumn.Gender)))?null:(System.String)reader[((int)CmsPatientColumn.Gender)];
			//entity.Gender = (Convert.IsDBNull(reader["Gender"]))?string.Empty:(System.String)reader["Gender"];
			entity.Nationality = (reader.IsDBNull(((int)CmsPatientColumn.Nationality)))?null:(System.String)reader[((int)CmsPatientColumn.Nationality)];
			//entity.Nationality = (Convert.IsDBNull(reader["Nationality"]))?string.Empty:(System.String)reader["Nationality"];
			reader.Close();
	
			entity.AcceptChanges();
		}
		
		/*
		/// <summary>
		/// Refreshes the <see cref="CmsPatient"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="CmsPatient"/> object.</param>
		protected static void RefreshEntity(DataSet dataSet, CmsPatient entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.PatientCode = (Convert.IsDBNull(dataRow["PatientCode"]))?string.Empty:(System.String)dataRow["PatientCode"];
			entity.MemberType = (Convert.IsDBNull(dataRow["MemberType"]))?string.Empty:(System.String)dataRow["MemberType"];
			entity.JobAddress = (Convert.IsDBNull(dataRow["JobAddress"]))?string.Empty:(System.String)dataRow["JobAddress"];
			entity.Company = (Convert.IsDBNull(dataRow["Company"]))?string.Empty:(System.String)dataRow["Company"];
			entity.DateOfBirth = (Convert.IsDBNull(dataRow["DateOfBirth"]))?DateTime.MinValue:(System.DateTime?)dataRow["DateOfBirth"];
			entity.FullName = (Convert.IsDBNull(dataRow["FullName"]))?string.Empty:(System.String)dataRow["FullName"];
			entity.Gender = (Convert.IsDBNull(dataRow["Gender"]))?string.Empty:(System.String)dataRow["Gender"];
			entity.Nationality = (Convert.IsDBNull(dataRow["Nationality"]))?string.Empty:(System.String)dataRow["Nationality"];
			entity.AcceptChanges();
		}
		*/
			
		#endregion Helper Functions
	}//end class

	#region CmsPatientFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;EntityColumn&gt;"/> class
	/// that is used exclusively with a <see cref="CmsPatient"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class CmsPatientFilterBuilder : SqlFilterBuilder<CmsPatientColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the CmsPatientFilterBuilder class.
		/// </summary>
		public CmsPatientFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the CmsPatientFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public CmsPatientFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the CmsPatientFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public CmsPatientFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion CmsPatientFilterBuilder

	#region CmsPatientParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;EntityColumn&gt;"/> class
	/// that is used exclusively with a <see cref="CmsPatient"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class CmsPatientParameterBuilder : ParameterizedSqlFilterBuilder<CmsPatientColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the CmsPatientParameterBuilder class.
		/// </summary>
		public CmsPatientParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the CmsPatientParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public CmsPatientParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the CmsPatientParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public CmsPatientParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion CmsPatientParameterBuilder
	
	#region CmsPatientSortBuilder
    
    /// <summary>
    /// A strongly-typed instance of the <see cref="SqlSortBuilder&lt;EntityColumn&gt;"/> class
	/// that is used exclusively with a <see cref="CmsPatient"/> object.
    /// </summary>
    [CLSCompliant(true)]
    public class CmsPatientSortBuilder : SqlSortBuilder<CmsPatientColumn>
    {
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the CmsPatientSqlSortBuilder class.
		/// </summary>
		public CmsPatientSortBuilder() : base() { }

		#endregion Constructors

    }    
    #endregion CmsPatientSortBuilder

} // end namespace
